<template>
    <el-container class="applet">
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="primary" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                        <el-button type="primary" icon="fa fa-plus" @click="create()" size="small"> 新增</el-button>
                        <el-button type="danger" icon="fa fa-trash" @click="multidel()" size="small"> 删除</el-button>
                        <el-badge alt="如果没有数据请去收集" title="如果没有数据请去收集" :value="listData.count_formid" class="item">
                            <el-button style="margin-left: 15px" size="small"> 预览数量</el-button>
                        </el-badge>
                    </div>
                    <div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input size="medium" v-model="params.title" placeholder="请输入标题"
                                      class="input-with-select">
                                <el-button type="primary" @click="search()" slot="append"
                                           icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                        <div class="cms-search-item">
                            <el-date-picker size="medium"
                                            v-model="date"
                                            align="right"
                                            type="daterange"
                                            unlink-panels
                                            range-separator="至"
                                            start-placeholder="开始日期"
                                            end-placeholder="结束日期"
                                            :picker-options="pickerOptions" @change="pickerChange">
                            </el-date-picker>
                        </div>
                        <div class="cms-search-item" style="width: 130px" v-if="userInfo.id==1">
                            <el-select size="medium" v-model="params.userid" >
                                <el-option label="管理员(全部)" value=""></el-option>
                                <el-option v-for="(manage,index) in admins" :key="index" :label="manage"
                                           :value="index"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.hot">
                                <el-option label="热门(全部)" value=""></el-option>
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="热门" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.lock">
                                <el-option label="锁定(全部)" value=""></el-option>
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="锁定" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.sort">
                                <el-option label="排序(默认)" value=""></el-option>
                                <el-option label="浏览量" value="1"></el-option>
                                <el-option label="分享量" value="2"></el-option>
                            </el-select>
                        </div>
                    </div>
                    <el-table ref="multipleHandle"
                              v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              @selection-change="handleSelectionChange"
                              tooltip-effect="dark" :data="listData.data" border>
                        <el-table-column align="center" type="selection" width="50"></el-table-column>
                        <el-table-column align="center" prop="id" label="编号" width="80"></el-table-column>
                        <el-table-column prop="thumb" label="封面" width="120">
                            <template slot-scope="scope">
                                <el-popover placement="right" trigger="click">
                                    <img :src="scope.row.thumb"/>
                                    <el-button slot="reference"
                                               style="border: none;height: 50px;padding: 0;overflow: hidden">
                                        <div v-if="scope.row.hot == 2" class="ribbon">
                                            <div class="wrap"><span class="ribbon6">热</span></div>
                                        </div>
                                        <img title="点击查看原图" alt="点击查看原图" :src="scope.row.thumb"
                                             style="height: 50px;width: 100px"/>
                                    </el-button>
                                </el-popover>
                            </template>
                        </el-table-column>
                        <el-table-column label="标题" width="300">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.lock == 1" type="primary" size="small">正常</el-tag>
                                <el-tag v-if="scope.row.lock == 2" type="danger" size="small">锁定</el-tag>
                                {{scope.row.title}}
                            </template>
                        </el-table-column>
                        <el-table-column align="center" prop="name" label="分类" width="70"></el-table-column>
                        <el-table-column align="center" prop="nickname" label="管理员" width="70"></el-table-column>
                        <el-table-column align="center" prop="share" label="分享量" width="80"></el-table-column>
                        <el-table-column align="center" prop="view" label="浏览量" width="80"></el-table-column>
                        <el-table-column align="center" prop="pre" label="占比" width="80"></el-table-column>
                        <el-table-column align="center" prop="status_text" label="状态" width="70">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.status == 1">上架</el-tag>
                                <el-tag v-else type="warning">下架</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column prop="created_at" label="日期" width="140">
                            <template slot-scope="scope">{{ scope.row.created_at ?
                                moment(scope.row.created_at).format('YYYY-MM-DD HH:mm') : ''}}
                            </template>
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row.id)" icon="fa fa-edit" type="primary" size="mini" plain>
                                    编辑
                                </el-button>
                                <el-button @click="preview(scope.row.id)" icon="fa fa-send" type="success" size="mini"
                                           plain> 预览
                                </el-button>
                                <el-button v-if="scope.row.status == 1" @click="status(scope.row.id,2)"
                                           icon="fa fa-lock"
                                           type="warning" size="mini" plain> 下架
                                </el-button>
                                <el-button v-if="scope.row.status == 2" @click="status(scope.row.id,1)"
                                           icon="fa fa-unlock-alt" type="primary" size="mini" plain> 上架
                                </el-button>
                                <el-button v-if="scope.row.id > 1" @click="destroy(scope.row.id)" icon="fa fa-trash"
                                           type="danger" size="mini" plain> 删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {
        setUpper,
        ArticlePush,
        ArticleDestroyAll,
        ArticleDestroy,
        ArticleList,
        ArticlePreview,
        OperateAll
    } from '@/utils/request';
    import Pagination from '@/components/pagination';

    export default {
        components: {Pagination},
        data() {
            return {
                userInfo:this.$storage.get('userinfo'),
                listData: [],//列表数据
                multipleSelection: [],//多选项
                date: '',
                params: {
                    page: 1,
                    title: '',
                    status: '',
                    sort: '',
                    hot: '',
                    lock: '',
                    start: '',
                    end: '',
                    userid: '',
                    applet: "apang"
                },
                admins: [],
                //日期筛选
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '对比昨天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
            }
        },
        activated() {
            this.$store.dispatch('setAPangActive', '/apang/article');
            this.list();
            this.adminAll();
        },
        computed: {
            ...mapGetters(['loading']),
        },
        methods: {
            //时间筛选
            pickerChange() {
                if (this.date) {
                    this.params.start = this.moment(this.date[0]).format('YYYY-MM-DD');
                    this.params.end = this.moment(this.date[1]).format('YYYY-MM-DD');
                } else {
                    this.params.start = '';
                    this.params.end = '';
                }
            },
            //预览
            preview(id) {
                this.$confirm('确定要推送模板消息到手机查看吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ArticlePreview({id: id, applet: this.params.applet}).then((response) => {
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.$message.success(response.msg);
                        }
                    });
                }).catch(() => {});
            },
            //所有角色
            adminAll() {
                OperateAll().then((res) => {
                    this.admins = res.data;
                })
            },
            //新增
            create() {
                this.$router.push({name: 'APangArticleUpdate'})
            },
            //编辑
            edit(id) {
                this.$router.push({name: 'APangArticleUpdate', query:{id:id}})
            },
            // 更新
            update(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        OperateUpdate(this.form).then((res) => {
                            if (res.code) {
                                this.$message.error(res.msg);
                            } else {
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose: () => {
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            // 多选删除
            multidel() {
                if (this.multipleSelection.length == 0) {
                    this.$message.error('请至少选择一项你要删除的项');
                    return false;
                }
                this.destroy(this.multipleSelection);
            },
            //删除
            destroy(id) {
                let msg;
                if (id.length) {
                    msg = '确定要删除选中的文章信息吗？';
                } else {
                    msg = '确定要删除该文章信息吗？';
                }
                this.$confirm(msg, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    customClass: 'cms-message-box',
                    type: 'warning'
                }).then(() => {
                    ArticleDestroy({id: id, applet: this.params.applet}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {
                });
            },
            // 上下架
            status(id, status) {
                setUpper({id: id, applet: this.params.applet, status: status}).then((res) => {
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.list();
                    }
                })

            },
            //筛选
            search() {
                this.params.page = 1;
                this.list();
            },
            //列表
            list() {
                this.$store.dispatch('setLoading', true);
                ArticleList({params: this.params}).then((res) => {
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            handleSelectionChange(val) {
                let select = [];
                if (val.length > 0) {
                    val.filter((item) => {
                        if (select.indexOf(item.id) === -1) {
                            select.push(item.id)
                        }
                    });
                    this.multipleSelection = select;
                } else {
                    this.multipleSelection = [];
                }
            }
        }
    }
</script>